package com.yawn;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * @author Created by yawn on 2017-12-09 15:54
 */
public class TestBpmnQuery {

    public static void main(String[] args) throws Exception {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repository = engine.getRepositoryService();

        // 部署流程文件
        Deployment deployment = repository.createDeployment().addClasspathResource("query.bpmn").deploy();

        // 获得流程定义
        ProcessDefinition processDefinition = repository
                .createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();

        // 查询流程模型
        InputStream inputStream = repository.getProcessModel(processDefinition.getId());
        int count = inputStream.available();
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        System.out.println(new String(bytes));

        // 查询流程文件图片
        InputStream is = repository.getProcessDiagram(processDefinition.getId());
        BufferedImage image = ImageIO.read(is);
        File file = new File("queryResult.png");
        if (!file.exists()) { file.createNewFile(); }
        FileOutputStream fos = new FileOutputStream(file);
        ImageIO.write(image, "png", fos);
        fos.close();
        is.close();
    }
}
